package com.yunji.product.service.impl;

import com.yunji.common.model.cache.ProductAttrCacheDTO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.bo.ProductAttrsListQueryBO;
import com.yunji.product.dao.IProductAttrsDao;
import com.yunji.product.domain.ProductAttrDO;
import com.yunji.product.service.IProductAttrsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.product.service.impl
 * @Title: ProductAttrsService
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-15 10:29:42      V1.0        HuaAo       新建类
 */
@Service
public class ProductAttrsService implements IProductAttrsService {

    @Resource
    IProductAttrsDao productAttrsDao;

    @Override
    public boolean create(ProductAttrDO productAttr) {
        productAttr.setBindId(null);
        return productAttrsDao.save(productAttr);
    }

    @Override
    public List<ProductAttrDO> search(ProductAttrsListQueryBO bo) {
        return productAttrsDao.search(bo);
    }

    @Override
    public int statsOfProductAttr(Long... ids) {
        return productAttrsDao.statsOfProductAttr(ids);
    }

    @Override
    public void deleteByProductIds(Long... ids) {
        productAttrsDao.deleteByProductIds(ids);
    }

    //FIXME 这里需要加缓存 通过productId 获取所有属性
    @Override
    public List<ProductAttrCacheDTO> listCacheByProductId(Long productId) {
        List<ProductAttrDO> productAttrDOS = productAttrsDao.listByProductId(productId);
        if (productAttrDOS != null)
            return productAttrDOS.stream().map(a -> {
                ProductAttrCacheDTO dto = new ProductAttrCacheDTO();
                BeanUtils.clone(a, dto);
                return dto;
            }).collect(Collectors.toList());
        return null;
    }
}